﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using SHomeWorkshop.LunarPresentation.Expressions;

namespace SHomeWorkshop.LunarPresentation.Commands
{
    public partial class Command
    {
        public static void MoveToNext()
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            LunarPresentation.Expressions.Expression curEx
                = Globals.selectedEditor.SelectedExpression;

            if (curEx is Expressions.SubPanel)
            {
                MessageBox.Show("子面板不支持“前后平移”操作！", Globals.appName,
                    MessageBoxButton.OK, MessageBoxImage.Warning);
                return;//子面板不支持Copy。
            }

            if (curEx is Expressions.BaseText)
            {
                Expressions.BaseText bt = curEx as Expressions.BaseText;
                if (bt.IsHeaderText && bt.Indent <= 3 && bt.IsCollapsed)
                {
                    Command.MoveCollapsedHeaderTextToNext(bt);
                    return;
                }
            }

            Command.MoveNormalExToNext(curEx);

            // 刷新父面板中所有可能存在的树型线。
            if (curEx.ParentPanel != null && curEx.ParentPanel.Direction == Orientation.Vertical)
            {
                curEx.ParentPanel.RefreshTreeLinesOfHeaderText();
            }
        }

        private static void MoveNormalExToNext(Expressions.Expression curEx)
        {
            if (curEx == null || curEx.ParentPanel == null) return;

            int nextVisibleExIndex = -1;
            int curIndex = curEx.ParentPanel.BasePanel.Children.IndexOf(curEx);
            Expressions.Expression nextVisibleEx = null;
            for (int i = curIndex + 1; i < curEx.ParentPanel.BasePanel.Children.Count; i++)
            {
                Expressions.Expression ex = curEx.ParentPanel.BasePanel.Children[i] as Expressions.Expression;
                if (ex.Visibility == Visibility.Visible)
                {
                    nextVisibleEx = ex;
                    nextVisibleExIndex = i;
                    break;
                }
            }

            if (nextVisibleExIndex == -1 || nextVisibleEx == null) return;

            if (nextVisibleEx is BaseText == false)
            {
                Command.MoveNormalExToNextNormalEx(curEx);
                return;
            }

            Expressions.BaseText nextBt = nextVisibleEx as Expressions.BaseText;

            List<Expressions.Expression> subIndentExpressions = nextBt.GetSubIndentExpressions();

            if (nextBt.IsHeaderText && nextBt.Indent <= 3 && nextBt.IsCollapsed &&
                subIndentExpressions != null && subIndentExpressions.Count > 0)
            {
                Command.MoveNormalExToNextCollapsedHeaderText(curEx, nextBt);
            }
            else
            {
                Command.MoveNormalExToNextNormalEx(curEx);
            }
        }

        private static void MoveNormalExToNextCollapsedHeaderText(Expressions.Expression curEx, BaseText nextBt)
        {
            MessageBox.Show("因为要跨过一个被折叠的标题文本块，此操作被禁止！",
                Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
            return;

            #region 过分复杂，足以导致程序崩溃。禁止进行此项操作。

            //if (curEx == null || nextBt == null) return;
            //if (curEx.ParentPanel != nextBt.ParentPanel || curEx.Parent != nextBt.Parent) return;
            //if (curEx == nextBt) return;

            //int newIndex = nextBt.ParentPanel.BasePanel.Children.IndexOf(nextBt);
            //XmlNode parentNode = curEx.XmlData.ParentNode;
            //int curIndex = parentNode.IndexOf(curEx.XmlData);
            //if (curIndex != curEx.ParentPanel.BasePanel.Children.IndexOf(curEx))
            //{
            //    return;
            //}

            //Expressions.Expression lastEx = nextBt.SubIndentExpressions[nextBt.SubIndentExpressions.Count - 1];
            //int destIndex = nextBt.ParentPanel.BasePanel.Children.IndexOf(lastEx);
            //if (curEx.XmlData.MoveTo(destIndex))
            //{
            //    ModifingItem mi = new ModifingItem();
            //    Action actDel = new Action(curEx.ExID, curIndex, curEx.GetType().Name, "DeleteExpression",
            //        curEx.XmlData.OuterXml, null, ActionType.Delete, curEx.ParentPanel.ExID);
            //    mi.Add(actDel);

            //    curEx.MoveTo(destIndex);

            //    Action actInsert = new Action(curEx.ExID, destIndex, curEx.GetType().Name, "InsertExpression",
            //        null, curEx.XmlData.OuterXml, ActionType.Insert, curEx.ParentPanel.ExID);
            //    mi.Add(actInsert);

            //    Action actNextCollapsed = new Action(nextBt.ExID, -1, nextBt.GetType().Name,
            //        "IsCollapsed", nextBt.IsCollapsed.ToString(), false.ToString());
            //    nextBt.IsCollapsed = false;
            //    mi.Add(actNextCollapsed);

            //    if (curEx is Expressions.BaseText)
            //    {
            //        Expressions.BaseText bt = curEx as Expressions.BaseText;
            //        if (bt.IsHeaderText)
            //        {
            //            bt.ParentPanel.RefreshAutoNumbersOfBaseText();
            //        }
            //    }

            //    curEx.Editor.Do(mi);
            //    curEx.Editor.IsModified = true;
            //}

            #endregion
        }

        private static void MoveNormalExToNextNormalEx(Expressions.Expression curEx)
        {
            SubPanel parentPanel = curEx.ParentPanel;
            int indexOfCurEx = parentPanel.BasePanel.Children.IndexOf(curEx);
            if (indexOfCurEx > parentPanel.BasePanel.Children.Count - 2) return;//已在最后一位，无法再后移。

            ModifingItem mi = new ModifingItem();
            Action action = new Action(curEx.ExID, indexOfCurEx, curEx.GetType().Name, indexOfCurEx.ToString(),
                "Index", (indexOfCurEx + 1).ToString(), ActionType.MoveToNext, curEx.ParentPanel.ExID);

            parentPanel.BasePanel.Children.Remove(curEx);
            curEx.XmlData.MoveToNext();
            parentPanel.BasePanel.Children.Insert(indexOfCurEx + 1, curEx);

            mi.Add(action);
            curEx.Editor.Do(mi);

            if (curEx is Expressions.BaseText)
            {
                Expressions.BaseText bt = curEx as Expressions.BaseText;
                if (bt.IsHeaderText)
                {
                    parentPanel.RefreshAutoNumbersOfBaseText();
                }
            }

            curEx.Editor.IsModified = true;
        }

        private static void MoveCollapsedHeaderTextToNext(Expressions.BaseText bt)
        {
            MessageBox.Show("当前文本块处于被折叠状态，不允许平移！", Globals.appName,
               MessageBoxButton.OK, MessageBoxImage.Warning);
            return;

            #region 过分复杂，会导致程序崩溃。
            //if (bt == null) return;
            //if (bt.IsHeaderText == false) return;
            //if (bt.Indent > 3) return;
            //if (bt.IsCollapsed == false) return;

            //int nextVisibleExIndex = -1;
            //int curIndex = bt.ParentPanel.BasePanel.Children.IndexOf(bt);
            //Expressions.Expression nextVisibleEx = null;
            //for (int i = curIndex + 1; i < bt.ParentPanel.BasePanel.Children.Count; i++)
            //{
            //    Expressions.Expression ex = bt.ParentPanel.BasePanel.Children[i] as Expressions.Expression;
            //    if (ex.Visibility == Visibility.Visible)
            //    {
            //        nextVisibleEx = ex;
            //        nextVisibleExIndex = i;
            //        break;
            //    }
            //}

            //if (nextVisibleExIndex == -1 || nextVisibleEx == null) return;

            //if (nextVisibleEx is BaseText == false)
            //{
            //    //如果下一个可见公式不是文本块。
            //    Command.MoveCollapsedHeaderTextToNextNormalEx(bt, nextVisibleEx);
            //    return;
            //}

            //Expressions.BaseText nextBt = nextVisibleEx as Expressions.BaseText;
            //if (nextBt.IsHeaderText && nextBt.Indent <= 3 && nextBt.IsCollapsed &&
            //    nextBt.SubIndentExpressions != null && nextBt.SubIndentExpressions.Count > 0)
            //{
            //    Command.MoveCollapsedHeaderTextToNextCollapsedHeaderText(bt, nextBt);
            //}
            //else
            //{
            //    //如果下一个可见公式是没有被折叠的文本块。
            //    Command.MoveCollapsedHeaderTextToNextNormalEx(bt, nextVisibleEx);
            //} 
            #endregion
        }

        #region 过分复杂的设计，足以导致程序崩溃。
        //private static void MoveCollapsedHeaderTextToNextNormalEx(BaseText bt, Expressions.Expression nextVisibleEx)
        //{
        //    if (bt == null || nextVisibleEx == null) return;
        //    if (bt.ParentPanel != nextVisibleEx.ParentPanel ||
        //        bt.Parent != nextVisibleEx.Parent) return;
        //    if (bt.ParentPanel.BasePanel == null) return;

        //    StackPanel sPanel = bt.ParentPanel.BasePanel;
        //    int btIndex = sPanel.Children.IndexOf(bt);

        //    if (nextVisibleEx.XmlData.MoveTo(btIndex))
        //    {
        //        ModifingItem mi = new ModifingItem() { OldSelectedExID = bt.ExID };

        //        Action actDel = new Action(nextVisibleEx.ExID,
        //            sPanel.Children.IndexOf(nextVisibleEx), nextVisibleEx.GetType().Name,
        //            "DeleteExpression", nextVisibleEx.XmlData.OuterXml, null, ActionType.Delete,
        //            bt.ParentPanel.ExID);

        //        sPanel.Children.Remove(nextVisibleEx);

        //        mi.Add(actDel);

        //        Action ActInsert = new Action(nextVisibleEx.ExID,
        //            btIndex, nextVisibleEx.GetType().Name, "InsertExpression", null,
        //            nextVisibleEx.XmlData.OuterXml, ActionType.Insert, bt.ParentPanel.ExID);

        //        sPanel.Children.Insert(btIndex, nextVisibleEx);

        //        mi.Add(ActInsert);

        //        bt.Editor.Do(mi);

        //        if (nextVisibleEx is Expressions.BaseText)
        //        {
        //            Expressions.BaseText btNext = nextVisibleEx as Expressions.BaseText;
        //            if (btNext.IsHeaderText)
        //            {
        //                btNext.ParentPanel.RefreshAutoNumbersOfBaseText();
        //            }
        //        }
        //        bt.ParentPanel.RefreshHeaderTextCollapsed();
        //        bt.Editor.IsModified = true;
        //    }
        //}

        //private static void MoveCollapsedHeaderTextToNextCollapsedHeaderText(BaseText bt, BaseText nextBt)
        //{
        //    if (bt == null || nextBt == null) return;
        //    if (bt.Parent != nextBt.Parent || bt.ParentPanel != nextBt.ParentPanel) return;

        //    StackPanel sPanel = bt.ParentPanel.BasePanel;
        //    int btIndex = sPanel.Children.IndexOf(bt);

        //    ModifingItem mi = new ModifingItem() { OldSelectedExID = bt.ExID };

        //    List<Expressions.Expression> subExs = nextBt.SubIndentExpressions;
        //    for (int i = subExs.Count - 1; i >= 0; i--)
        //    {
        //        Expressions.Expression subEx = subExs[i];
        //        if (subEx.XmlData.MoveTo(btIndex))
        //        {
        //            Action actDel = new Action(subEx.ExID, sPanel.Children.IndexOf(subEx),
        //                subEx.GetType().Name, "DeleteExpression", subEx.XmlData.OuterXml, null,
        //                ActionType.Delete, bt.ParentPanel.ExID);

        //            sPanel.Children.Remove(subEx);

        //            mi.Add(actDel);

        //            Action actInsert = new Action(subEx.ExID, btIndex, subEx.GetType().Name,
        //                "InsertExpression", null, subEx.XmlData.OuterXml, ActionType.Insert,
        //                bt.ParentPanel.ExID);

        //            sPanel.Children.Insert(btIndex, subEx);

        //            mi.Add(actInsert);
        //        }
        //    }

        //    if (nextBt.XmlData.MoveTo(btIndex))
        //    {
        //        Action actDel = new Action(nextBt.ExID, sPanel.Children.IndexOf(nextBt),
        //            nextBt.GetType().Name, "DeleteExpression", nextBt.XmlData.OuterXml, null,
        //            ActionType.Delete, bt.ParentPanel.ExID);

        //        sPanel.Children.Remove(nextBt);

        //        mi.Add(actDel);

        //        Action actInsert = new Action(nextBt.ExID, btIndex, nextBt.GetType().Name,
        //            "InsertExpression", null, nextBt.XmlData.OuterXml, ActionType.Insert,
        //            bt.ParentPanel.ExID);

        //        sPanel.Children.Insert(btIndex, nextBt);

        //        mi.Add(actInsert);

        //        Action actNextCollapsed = new Action(nextBt.ExID, -1, nextBt.GetType().Name,
        //            "IsCollapsed", nextBt.IsCollapsed.ToString(), false.ToString());
        //        nextBt.IsCollapsed = false;
        //        mi.Add(actNextCollapsed);

        //        if (bt is Expressions.BaseText)
        //        {
        //            if (bt.IsHeaderText)
        //            {
        //                bt.ParentPanel.RefreshAutoNumbersOfBaseText();
        //            }
        //        }

        //        bt.Editor.Do(mi);

        //        bt.ParentPanel.RefreshHeaderTextCollapsed();
        //        bt.Editor.IsModified = true;
        //    }
        //}
        #endregion
    }
}
